<template>
  <div class="pagination">
    <button :disabled="currentPage === 1" @click="$emit('changePage',currentPage - 1)">上一页</button>

    <button v-if="startEnd.start > 1" @click="$emit('changePage',1)" >1</button>

    <button v-if="startEnd.start > 2" >···</button>

    <button :class="{active: currentPage === page}" v-for="page in startEnd.end" :key="page" v-show="page >= startEnd.start" @click="$emit('changePage',page)">{{page}}</button>

    <button v-if="startEnd.end < pageTotal - 1">···</button>

    <button v-if="startEnd.end < pageTotal" @click="$emit('changePage',pageTotal)">{{pageTotal}}</button>

    <button :disabled="currentPage === pageTotal" @click="$emit('changePage',currentPage + 1)">下一页</button>

    <button style="margin-left: 30px">共 {{total}} 条</button>
  </div>
</template>

  

  <script>
export default {
  name: "Pagination",
  props: {
    currentPage: {
      type: Number,
      default: 1,
    },
    total: {
      type: Number,
      default: 0,
    },
    pageSize: {
      type: Number,
      default: 0,
    },
    continueNum: {
      type: Number,
      default: 3,
    },
  },
  computed: {
    pageTotal() {
      return Math.ceil(this.total / this.pageSize) 
    },
    startEnd() {
      let start = 0
      let end = 0
      const {currentPage,pageTotal,continueNum} = this
      if(continueNum > pageTotal) {
        start = 1
        end = pageTotal
      }else {
        start = currentPage - Math.floor(continueNum / 2)
        end = currentPage + Math.floor(continueNum / 2)
        if(start < 1) {
          start = 1
          end = continueNum
        }else if(end > pageTotal) {
          end = pageTotal
          start = pageTotal - continueNum + 1
        }
      }
      return {start,end}
    }
  }
};
</script>

  

  <style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;

    background-color: #f4f4f5;

    color: #606266;

    outline: none;

    border-radius: 2px;

    padding: 0 4px;

    vertical-align: top;

    display: inline-block;

    font-size: 13px;

    min-width: 35.5px;

    height: 28px;

    line-height: 28px;

    cursor: pointer;

    box-sizing: border-box;

    text-align: center;

    border: 0;

    &[disabled] {
      color: #c0c4cc;

      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;

      background-color: #409eff;

      color: #fff;
    }
  }
}
</style>